/*
 * Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */


#include <arch/scr.h>
#include <arch/cpsr.h>
#include <stdlib/stdint.h>
#include <core/dprintf.h>

void cpsr_dump_info()
{
    unsigned int cpsr_value      = cpsr_nzcv_daif();
    int32_t      exception_level = get_current_elx();
    switch (exception_level) {
        case 0:
            break;
        case 1:
            cpsr_value |= scr_read_SCTLR_EL1() & (1 << CPSR_BIT_EE);
            break;
        case 2:
            cpsr_value |= scr_read_SCTLR_EL2() & (1 << CPSR_BIT_EE);
            break;
        case 3:
            cpsr_value |= scr_read_SCTLR_EL3() & (1 << CPSR_BIT_EE);
            break;
        default:
            dprintf("error exception_level!\n");
    }
    dprintf("exception_level:EL%d\n", exception_level);

    dprintf("Cpsr:%x\n", cpsr_value);

    dprintf("Endian: %s\n", (cpsr_value & bitmask(CPSR_BIT_EE)) > 0 ? "Big" : "Little");

    dprintf("System Error: %s\n", (cpsr_value & bitmask(CPSR_BIT_A)) > 0 ? "Masked" : "Enabled");

    dprintf("IRQ: %s, FIQ: %s\n", (cpsr_value & bitmask(CPSR_BIT_I)) > 0 ? "Masked" : "Enabled",
            (cpsr_value & bitmask(CPSR_BIT_F)) > 0 ? "Masked" : "Enabled");

    dprintf("Execution state:%s\n", (cpsr_value & bitmask(CPSR_BIT_M)) > 0 ? "AArch32" : "AArch64");

    dprintf("SCTLR_EL1:%x\n", scr_read_SCTLR_EL1());
}
void current_elx()
{
    int32_t exception_level = get_current_elx();
    dprintf("exception_level:EL%d\n", exception_level);
}
